<?php

	class user extends core{

		protected $table_user = 'syt_user';//会员表



		/**
		* 构造函数
		*
		* @return void
		*/
		public function __construct() {

		}

		/**
		* 析构函数
		*
		* @return void
		*/
		public function __destruct() {

		}

		/**
		* 根据手机号查询会员信息
		* @author xiaxin 2014-04-09
		* @param mixed $mobile
		*/
		public function get_user_by_phone($mobile){
			if(!is_numeric($mobile)){
				return false;
			}
			$sql = "SELECT a.*,c.city_name as city_name FROM {$this->table_user} as a left join {$this->table_city} as c On c.id=a.area WHERE a.phone='{$mobile}' AND a.state=1";
			return $this->_mysql(0)->findone($sql);
		}

		/**
		* 会员注册
		*
		* @param mixed $data
		* @return mysql对象
		*/
		public function reg_user($data){
			if(!$data){
				return false;
			}
			return $this->set_db($this->table_user,$data);
		}
		/**
		* 会员修改
		*
		* @param mixed $data
		* @return mysql对象
		*/
		public function edit_user($data,$uid){
			if(!$data || !$uid){
				return false;
			}
			return $this->set_db($this->table_user,$data,false,'id='.$uid);
		}

		/**
		* 写入积分日志
		* @author xiaxin 2014-04-09
		* @param mixed $data
		*/
		public function user_point_log($data){
			if(!$data){
				return false;
			}
			return $this->set_db($this->table_point_log,$data);
		}

		/**
		* 更新会员
		* @author xiezhihui 14-04-11 05:19
		* @param mixed $data
		*/
		public function user_update($id,$data){
			if(!$id){
				return false;
			}
			return $this->set_db($this->table_user,$data,false,'id='.$id);
		}

		/**
		* 根据手机号和密码取得会员信息
		* @author xiaxin 2014-04-09
		* @param mixed $phone
		* @param string $password
		*/
		public function out_login($phone,$password){
			if(!is_numeric($phone) || !$password){
				return false;
			}
			//$password = md5($password); //密码md5加密  必须是之前传入前md5
			$sql = "SELECT * FROM {$this->table_user} WHERE `phone`='{$phone}' AND `password`='{$password}'";
			$row = $this->_mysql(0)->findone($sql);
			return $row;
		}

		/**
		* 根据id查询会员信息
		*
		* @param mixed $uid
		*/
		public function get_user_by_id($uid){
			if(!is_numeric($uid)){
				return false;
			}
			$sql = "SELECT * FROM {$this->table_user} WHERE id={$uid} limit 1";
			return $this->_mysql(0)->findone($sql);
		}

		/**
		* 更新用户信息
		* @author xiezhihui 14-04-09 03:12
		* @param mixed $data
		*/
		public function add_point($data){
			if(!$data){
				return false;
			}
			$sql = "Update {$this->table_user} SET `point`=`point`+{$data['point']}, `total_point`=`total_point`+{$data['point']}  where phone='{$data['phone']}'";
			return $this->_mysql(0)->update($sql);

		}
		/**
		* 获取用户列表
		* @author xiezhihui 14-04-12 10:24
		* @param mixed $data
		* @param mixed $page
		* @param mixed $pagesize
		* @return mysql对象
		*/
		public function get_user_list($data,$page=1,$pagesize=10){
			$sql="SELECT a.*,b.city_name as b_city_name FROM {$this->table_user} as a Left Join {$this->table_city} as b On a.area=b.id WHERE 1";
			$sql_cond='';

			if($data['state']){
				$sql_cond.=" and a.state='{$data['state']}'";
			}
			if($data['title']){
				$sql_cond.=" and (a.phone like '%{$data['title']}%' or a.name like '%{$data['title']}%')";
			}

			//城市处理 该顺序不能乱
			$sql_city="";
			if($data['province']){
				$sql_city=" and a.area like '".substr($data['province'],0,2).'____'."'";
			}if($data['city']){
				$zx_city=$data['zx_city'];
				$sql_city=" and a.area like '".( (in_array( substr($data['city'],0,2).'0000',$zx_city )?substr($data['city'],0,2).'____':substr($data['city'],0,4).'__') )."'";
			}
			if($data['area']){
				$sql_city=" and a.area like '".$data['area']."'";
			}
			$sql.=$sql_cond.$sql_city;
			$sql_count="SELECT count(a.id) as count FROM {$this->table_user} as a Left Join {$this->table_city} as b On a.area=b.id WHERE 1 {$sql_cond}{$sql_city} limit 1";
			$count = $this->_mysql(0)->findone($sql_count);
			$res = $this->_mysql(0)->findlimit($sql,$page,$pagesize,$count['count']);
			return $res;
		}
		/**
		* 日志列表
		* @author xiezhihui 14-04-15 09:02
		* @param mixed $data
		* @param mixed $page
		* @param mixed $pagesize
		* @return mysql对象
		*/
		public function get_logs_list($data,$page=1,$pagesize=10){
			$sql = "SELECT a.*,b.name as u_name,b.phone,d.addr,d.name as s_name,e.city_name from {$this->table_point_log} as a Left join {$this->table_user} as b On a.uid=b.id Left Join {$this->table_pos_auth} as c On a.pos_id=c.id Left Join {$this->table_shop} as d On c.shop_id=d.id Left Join {$this->table_city} as e On d.area=e.id where 1";
			$sql_cond='';
			if($data['type']){
				$sql_cond.=" and a.type='{$data['type']}'";
			}
			if($data['title']){
				$sql_cond.=" and (b.phone like '%{$data['title']}%' or b.name like '%{$data['title']}%' or a.obj like '%{$data['title']}%')";
			}
			if($data['uid']){//根据会员ID号查找消费日志 xiaxin 2014-06-03
				$sql_cond .= " and a.uid={$data['uid']}";
			}

			//城市处理 该顺序不能乱
			$sql_city="";
			if($data['province']){
				$sql_city=" and d.area like '".substr($data['province'],0,2).'____'."'";
			}if($data['city']){
				$zx_city=$data['zx_city'];
				$sql_city=" and d.area like '".( (in_array( substr($data['city'],0,2).'0000',$zx_city )?substr($data['city'],0,2).'____':substr($data['city'],0,4).'__') )."'";
			}
			if($data['area']){
				$sql_city=" and d.area like '".$data['area']."'";
			}
			$sql.=$sql_cond.$sql_city .' order by a.handle_time desc ,a.create_time desc ';

			$sql_count="SELECT count(a.id) as count from {$this->table_point_log} as a Left join {$this->table_user} as b On a.uid=b.id Left Join {$this->table_pos_auth} as c On a.pos_id=c.id Left Join {$this->table_shop} as d On c.shop_id=d.id Left Join {$this->table_city} as e On d.area=e.id WHERE 1 {$sql_cond}{$sql_city} limit 1";
			$count = $this->_mysql(0)->findone($sql_count);

			//统计总积分
			if($data['type']!=1){
			//获赠的总积分
			$sql_count_add_point="SELECT sum(a.point) as sum_point from {$this->table_point_log} as a Left join {$this->table_user} as b On a.uid=b.id Left Join {$this->table_pos_auth} as c On a.pos_id=c.id Left Join {$this->table_shop} as d On c.shop_id=d.id Left Join {$this->table_city} as e On d.area=e.id WHERE 1 {$sql_cond}{$sql_city} and a.type=2 limit 1";
			$add_point = $this->_mysql(0)->findone($sql_count_add_point);
			}
			if($data['type']!=2){
			//消费的总积分
			$sql_count_minus_point="SELECT sum(a.point) as sum_point from {$this->table_point_log} as a Left join {$this->table_user} as b On a.uid=b.id Left Join {$this->table_pos_auth} as c On a.pos_id=c.id Left Join {$this->table_shop} as d On c.shop_id=d.id Left Join {$this->table_city} as e On d.area=e.id WHERE 1 {$sql_cond}{$sql_city} and a.type=1 limit 1";
			$minus_point = $this->_mysql(0)->findone($sql_count_minus_point);
			}
			$res = $this->_mysql(0)->findlimit($sql,$page,$pagesize,$count['count']);
			$res['count_array']=array(
				'total_add_point'=>$add_point['sum_point']?$add_point['sum_point']:0,
				'total_minus_point'=>$minus_point['sum_point']?$minus_point['sum_point']:0,
			);
			return $res;
		}
		/**
		* 获取一条日志记录
		* @author xiezhihui 14-04-15 09:04
		* @param mixed $id
		*/
		public function get_logs_info($id){
			if(!$id){
				return false;
			}
			$sql = "SELECT a.*,b.name as u_name,b.phone,d.addr,d.name as s_name,e.city_name from {$this->table_point_log} as a Left join {$this->table_user} as b On a.uid=b.id Left Join {$this->table_pos_auth} as c On a.pos_id=c.id Left Join {$this->table_shop} as d On c.shop_id=d.id Left Join {$this->table_city} as e On d.area=e.id  WHERE a.id={$id} limit 1";
			return $this->_mysql()->findone($sql);
		}

		/**
		* 获取user登录后的session
		*
		*/
		public function _session_userinfo(){
			return unserialize(get_session('user'));
		}

		/**
		* 添加会员的折扣信息
		* @author xiaxin 2014-06-06
		*
		* @param mixed $data
		*/
		public function discount_add($data){
			if(!$data){
				return false;
			}
			return $this->set_db($this->table_vip_discount,$data);
		}

		/**
		* 修改会员的折扣信息
		* @author xiaxin 2014-06-06
		* @param array $data
		* @param int $id
		*/
		public function discount_edit($data,$id){
			if(!$id || !$data){
				return false;
			}
			$where = "id={$id}";
			return $this->set_db($this->table_vip_discount,$data,false,$where);
		}

		/**
		* 删除会员折扣信息
		* @author xiaxin 2014-06-06
		* @param mixed $id
		*/
		public function discount_del($id){
			if(!is_numeric($id)){
				return false;
			}
			$sql = "delete from {$this->table_vip_discount} where id={$id}";
			return $this->_mysql(0)->update($sql);
		}


	}
?>
